package com.ysd.ServiceImpl;

import java.util.List;


import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;


import com.ysd.Service.UserInfoService;
import com.ysd.entity.Modules;
import com.ysd.entity.UserInfo;
import com.ysd.repository.UserInfoRepository;
import com.ysd.util.Treemodule;


@Service
public class UserInfoServiceImpl implements UserInfoService {

	@Autowired
	private UserInfoRepository userInfoRepository;
	
	

	@Override
	public UserInfo findByUname(String uname) {
		// TODO Auto-generated method stub
		return userInfoRepository.findByUname(uname);
	}

	@Override
	public UserInfo addAndUpdate(UserInfo u) {
		// TODO Auto-generated method stub
		return userInfoRepository.saveAndFlush(u);
	}
	/**
	 *根据用户id查询用户
	 */
	@Override
	public UserInfo findUsersById(String uid) {
		// TODO Auto-generated method stub
		return userInfoRepository.findById(uid).get();
	}
	/** 权限
	 * 获取该用户的所有模块
	 * 
	 * @param modules
	 * @return
	 */
	public List<Treemodule> findUserModules(List<Modules> modules) {
		// TODO Auto-generated method stub
		return this.getModulesTree(modules);
	}

	private List<Treemodule> getModulesTree(List<Modules> modules) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<UserInfo> findAllUsers(UserInfo u, String did, Pageable pageable) {
		// TODO Auto-generated method stub
		return userInfoRepository.findAll(get(u,did),pageable);
	}



	private Specification<UserInfo> get(UserInfo u, String did) {
		// TODO Auto-generated method stub
		return new Specification<UserInfo>() {

			@Override
			public Predicate toPredicate(Root<UserInfo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				// TODO Auto-generated method stub
				Predicate predicate = criteriaBuilder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				if (u.getUname()!=null && u.getUname()!="") {//用户名
					expressions.add(criteriaBuilder.like(root.get("uname"),"%"+u.getUname()+"%" ));
					}
				if (did!=null) {//部门
					expressions.add(criteriaBuilder.equal(root.get("department").get("did"), did));
					}
				if (u.getPhone()!=null && u.getPhone()!="") {//手机号
					expressions.add(criteriaBuilder.like(root.get("phone"),"%"+u.getPhone()+"%" ));
					}
				if (u.getEmail()!=null && u.getEmail()!="") {//邮箱
					expressions.add(criteriaBuilder.like(root.get("email"),"%"+u.getEmail()+"%" ));
					}
				return predicate;
			}
			
		};
	}

	@Override
	public void deleteUsers(UserInfo users) {
		// TODO Auto-generated method stub
		userInfoRepository.delete(users);
	}

	@Override
	public Integer ResetPasscheck(String uid) {
		// TODO Auto-generated method stub
		return userInfoRepository.ResetPasscheck(uid);
	}

	@Override
	public UserInfo pduname(String uname) {
		// TODO Auto-generated method stub
		return userInfoRepository.pduname(uname);
	}
	

	@Override
	public void saveUserrole(String userid, String roleid) {
		userInfoRepository.saveUserrole(userid, roleid);
	}

	@Override
	public void delUserrole(String userid, String roleid) {
		userInfoRepository.delUserrole(userid, roleid);
		
	}

}
